package com.hl.im.server.controller;

import com.hl.im.server.infrastrructure.session.ISessionStore;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;

/**
 * @author huanglin
 * @apiNote
 * @date: 2023/09/09 11:40
 */
@RestController
@RequestMapping(value = "websocket")
@RequiredArgsConstructor
public class WebsocketController {
    private final ISessionStore sessionStore;

    @PostMapping(value = "sendMsg")
    @SneakyThrows
    public String sendMsg(@RequestBody PushMessage message) {
        TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(
                Instant.now().toEpochMilli() + ":" + message.getMessage());
        ChannelGroupFuture cgf = sessionStore.channelGroup().writeAndFlush(textWebSocketFrame);
        cgf.sync();
        return "发送成功";
    }

    @Data
    static class PushMessage {
        private String message;
    }
}
